package dao;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/**
 * 工具类:
 * 打开链接,释放资源,通用的增删改,查询
 */
public class BaseDao {
    private static String driver = null;
    private static String url = null;
    private static String username = null;
    private static String password = null;
    protected Connection connection = null;
    protected PreparedStatement pstmt = null;
    protected ResultSet rs = null;
    static Properties properties = new Properties();
    static {
        InputStream inputStream = BaseDao.class.getClassLoader().getResourceAsStream("database.properties");
        try {
            properties.load(inputStream);
            driver = properties.getProperty("driver");
            url = properties.getProperty("url");
            username = properties.getProperty("username");
            password = properties.getProperty("password");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 通用获取链接的方法
     * @return
     */
    public Connection getConnection(){
        try {
            Class.forName(driver);
            connection = DriverManager.getConnection(url,username,password);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return connection;
    }

    /**
     * 释放资源的方法
     */
    public void closeAll(Connection conn, PreparedStatement pstmt, ResultSet rs){
            try {
                if(conn!=null){
                    conn.close();
                }
                if(pstmt != null){
                    pstmt.close();
                }
                if(rs!=null){
                    rs.close();
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
    }

    /**
     * 通用的增删改的方法
     * @param sql
     * @param params
     * @return
     */
    public int executeUpdate(String sql,Object[] params){
        int count = 0;
        connection = getConnection();
        try {
            pstmt = connection.prepareStatement(sql);
            if(params.length != 0 && params != null){
                for(int i = 0;i<params.length;i++){
                    pstmt.setObject(i+1,params[i]);
                }
                count = pstmt.executeUpdate();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll(connection,pstmt,null);
        }
        return count;
    }

    /**
     * 通用的查询的方法
     * ... 可变参数
     * 参数可以是0个,1个,2个,```````
     *
     * @param sql
     * @param params
     * @return
     */
    public ResultSet executeQuery(String sql,Object... params){
        connection = getConnection();
        try {
            pstmt = connection.prepareStatement(sql);
            for(int i = 0;i<params.length;i++){
                pstmt.setObject(i+1,params[i]);
            }
            rs = pstmt.executeQuery();

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return rs;
    }

}
